package com.ybc.chinese.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.ybc.chinese.CustomShareListener;
import com.ybc.chinese.R;
import com.ybc.chinese.bean.Detail;
import com.ybc.chinese.utils.CacheUtil;
import com.ybc.chinese.utils.CommonUtil;
import com.ybc.chinese.utils.ConfigHelper;
import com.ybc.chinese.utils.Contants;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * <p>详情(通用)<p/>
 * Created by Yubch on 17/2/16.
 */
public class DetailActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_left)
    ImageView ivLeft;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.iv_right)
    ImageView ivRight;

    private static final String INDEX_DETAIL = "index/detail";
    private Context mCtx;
    private String mTitle;
    private UMShareListener mShareListener;
    private ShareAction mShareAction;
    private UMWeb web;
    private int fontSize;

    public static void newIntent(Context context, String title) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra(Contants.INTENT_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        mCtx = this;
        ButterKnife.bind(this);
        mTitle = getIntent().getStringExtra(Contants.INTENT_TITLE);
        initView();
        String content = CacheUtil.getCache(mCtx, INDEX_DETAIL + mTitle);
        if(TextUtils.isEmpty(content)) {
            requestDetail();
        } else {
            tvContent.setText(content);
        }

    }


    private void requestDetail() {
        if (!CommonUtil.isNetworkConnected(mCtx)) {
            CommonUtil.showToast(mCtx, R.string.no_net);
            return;
        }
        showRunningDialog();
        BmobQuery<Detail> query = new BmobQuery<>();
        query.addWhereEqualTo("title", mTitle);
        //执行查询，第一个参数为上下文，第二个参数为查找的回调
        query.findObjects(new FindListener<Detail>() {

            @Override
            public void done(List<Detail> list, BmobException e) {
                if (e == null) {
                    if(!CommonUtil.isEmpty(list)) {
                        tvContent.setText(list.get(0).getContent());
                        CacheUtil.saveCache(mCtx, INDEX_DETAIL + mTitle, list.get(0).getContent());
                    }

                } else {
                    CommonUtil.showToast(mCtx, "数据请求失败");
                }
                dismissRunningDialog();
            }
        });
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.ic_back);
        ivLeft.setVisibility(View.VISIBLE);
        tvTitle.setText(mTitle);
        ivRight.setImageResource(R.mipmap.ic_share);
        ivRight.setVisibility(View.VISIBLE);
        mShareListener = new CustomShareListener(DetailActivity.this);
        web = new UMWeb("http://a.app.qq.com/o/simple.jsp?pkgname=com.ybc.chinese");
        web.setTitle(getString(R.string.app_name));
        web.setThumb(new UMImage(this, R.mipmap.ic_logo));
        web.setDescription("最专业的"+getString(R.string.app_name)+"学习软件");
        /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                /*SHARE_MEDIA.SINA, */SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        new ShareAction(DetailActivity.this).withMedia(web).withText("有了它，妈妈再也不用担心我的学习了")
                                .setPlatform(share_media)
                                .setCallback(mShareListener)
                                .share();
                    }
                });
        fontSize = ConfigHelper.getInstance(mCtx).loadIntKey(ConfigHelper.DETAIL_FONT_SIZE, 15);
        tvContent.setTextSize(fontSize);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.iv_left)
    public void left() {
        finish();
    }

    @OnClick(R.id.iv_right)
    public void right() {
        mShareAction.open();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(DetailActivity.this).onActivityResult(requestCode, resultCode, data);
    }

    @OnClick(R.id.tv_collection)
    public void collection() {
        String jsonCollection = CacheUtil.getCache(getApplicationContext(), CollectionActivity.COLLECTION_INDEX);
        List<String> collections = new ArrayList<>();
        if(!TextUtils.isEmpty(jsonCollection)) {
            collections = new Gson().fromJson(jsonCollection, new TypeToken<List<String>>(){}.getType());
        }
        if(collections.contains(mTitle)) {
            CommonUtil.showToast(mCtx, R.string.collectioned);
        } else {
            collections.add(mTitle);
            CacheUtil.saveCache(mCtx, CollectionActivity.COLLECTION_INDEX, new Gson().toJson(collections));
            CommonUtil.showToast(mCtx, R.string.collection_suc);
        }
    }

    @OnClick(R.id.tv_more_small)
    public void small() {
        fontSize--;
        tvContent.setTextSize(fontSize);
        ConfigHelper.getInstance(mCtx).saveIntKey(ConfigHelper.DETAIL_FONT_SIZE, fontSize);
    }
    @OnClick(R.id.tv_more_big)
    public void big() {
        fontSize++;
        tvContent.setTextSize(fontSize);
        ConfigHelper.getInstance(mCtx).saveIntKey(ConfigHelper.DETAIL_FONT_SIZE, fontSize);
    }
}
